﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using saigonbook.Biz;

namespace quanlybansach.Frm
{
    public partial class Frm_thongketonkho : Form
    {
        private List<BIZ_tonkho> danhsachtonkho = new List<BIZ_tonkho>();
        public Frm_thongketonkho()
        {
            InitializeComponent();
            DataGV_danhsachtonkho.Columns.Add("Col1", "STT");
            DataGV_danhsachtonkho.Columns.Add("Col2", "Mã sách");
            DataGV_danhsachtonkho.Columns.Add("Col3", "Tên sách");
            DataGV_danhsachtonkho.Columns.Add("Col4", "Loại sách");
            DataGV_danhsachtonkho.Columns.Add("Col5", "Giá bán (VNĐ)");
            DataGV_danhsachtonkho.Columns.Add("Col6", "Số lượng");
            DataGV_danhsachtonkho.Columns[0].Width = 40;
            DataGV_danhsachtonkho.Columns[1].Width = 90;
            DataGV_danhsachtonkho.Columns[2].Width = 160;
            DataGV_danhsachtonkho.Columns[3].Width = 100;
            DataGV_danhsachtonkho.Columns[4].Width = 120;
            DataGV_danhsachtonkho.Columns[5].Width = 90;
            DataGV_danhsachtonkho.AllowUserToAddRows = false;
        }

        private void Frm_thongketonkho_Load(object sender, EventArgs e)
        {
            int stt = 0;
            int tongsoluongtonkho = 0;
            DataGV_danhsachtonkho.Rows.Clear();
            BIZ_tonkho tonkho = new BIZ_tonkho();
            danhsachtonkho = tonkho.Select_tonkho(DateTime.Now);
            foreach (BIZ_tonkho tk in danhsachtonkho)
            {
                stt++;
                tongsoluongtonkho += tk.soluong;
                string gia = tk.sach.giaban.ToString("c");
                gia = gia.Substring(1, gia.LastIndexOf(".") - 1);
                DataGV_danhsachtonkho.Rows.Add(stt, tk.sach.masach, tk.sach.tensach, tk.sach.loai.tenloai, gia, tk.soluong);
            }
            cbb_loai = BIZ_sach.Cbb_loaisach(cbb_loai);
            cbb_loai.Text = "Tất cả";
            cbbtensach = BIZ_sach.Cbb_sach(cbbtensach);
            txtongsoluong.Text = tongsoluongtonkho.ToString() + " quyển";
        }

        private void btnxem_Click(object sender, EventArgs e)
        {
            int stt = 0;
            int tongsoluongtonkho = 0;
            DataGV_danhsachtonkho.Rows.Clear();
            BIZ_tonkho tonkho = new BIZ_tonkho();
            if (cbb_loai.Text.Equals("Tất cả"))
                danhsachtonkho = tonkho.Select_tonkho(ngayxem.Value.Date);
            else
                danhsachtonkho = tonkho.Select_tonkho(ngayxem.Value.Date, int.Parse(cbb_loai.SelectedValue.ToString()));
            foreach (BIZ_tonkho tk in danhsachtonkho)
            {
                stt++;
                tongsoluongtonkho += tk.soluong;
                string gia = tk.sach.giaban.ToString("c");
                gia = gia.Substring(1, gia.LastIndexOf(".") - 1);
                DataGV_danhsachtonkho.Rows.Add(stt, tk.sach.masach, tk.sach.tensach, tk.sach.loai.tenloai, gia, tk.soluong);
            }
            txtongsoluong.Text = tongsoluongtonkho.ToString() + " quyển";
            if (DataGV_danhsachtonkho.RowCount < 1)
                btnxuatbaocao.Enabled = false;
            else
                btnxuatbaocao.Enabled = true;
        }

        private void btndong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnthongke_Click(object sender, EventArgs e)
        {
            BIZ_tonkho tonkho = new BIZ_tonkho();
            tonkho.ngay = ngaythongke.Value.Date;
            tonkho.sach = new BIZ_sach();
            tonkho.sach.masach = int.Parse(cbbtensach.SelectedValue.ToString());
            txtsoluongton.Text = tonkho.soluongtonkho(tonkho).ToString();
            BIZ_phieunhap pn = new BIZ_phieunhap();
            txtsoluongnhap.Text = pn.soluongnhaptrongngay(tonkho.sach.masach, tonkho.ngay).ToString();
            BIZ_donhang dh = new BIZ_donhang();
            txtsoluongban.Text = dh.soluongbantrongngay(tonkho.sach.masach, tonkho.ngay).ToString();
        }

        private void btnxuatbaocao_Click(object sender, EventArgs e)
        {
                Report rp = new Report();
                rp.ExportToExcel_tonkho(ngayxem, cbb_loai, DataGV_danhsachtonkho, txtongsoluong);
        }
    }
}
